import os

from pynput import mouse, keyboard
import threading
import time

from src.config.configLoader import ConfigLoader
from src.modules.bar.barResources import BarResources
from src.modules.bar.barStatus import BarStatus
from src.modules.bar.barValue import BarValue


from src.modules.gameWindow import GameWindow
from src.modules.skill import Skill

# 标记鼠标侧键1是否被按下
side_button_pressed = False

# 创建一个键盘控制器
keyboard_controller = keyboard.Controller()

mouse_controller = mouse.Controller()

# 创建一个事件对象
key_event = threading.Event()


def mouse_listener():
    global side_button_pressed
    with mouse.Listener(on_click=on_click) as listener:
        listener.join()


# 侧键启动信号监听
def on_click(x, y, button, pressed):
    global side_button_pressed
    if button == mouse.Button.x2 and pressed:
        # side_button_pressed = pressed
        side_button_pressed = not side_button_pressed
        if side_button_pressed:
            key_event.set()  # 设置事件
        else:
            key_event.clear()  # 清除事件


# 主循环进行键盘技能控制
def key_press_loop():
    global side_button_pressed

    import os

    # 获取当前文件所在目录
    current_dir = os.path.dirname(os.path.abspath(__file__))

    # 方法：向上回溯两级（../../）
    # 假设从当前文件向上两层就是项目根目录
    project_root = os.path.dirname(os.path.dirname(current_dir))  # 回到项目根目录

    # 构建配置文件路径
    config_path = os.path.join(project_root, "config.json")

    gameWindow = GameWindow()

    # === 启动时加载配置 ===
    config_loader = ConfigLoader(config_path=config_path, project_root=project_root)

    # 选择分辨率模式
    resolution = "2k_juhe"  # 或 "2k"

    # === 初始化各个模块 ===

    # bar = BarValue(gameWindow, config_loader.get_section("barValue", resolution))
    bar_status = BarStatus(gameWindow, config_loader.get_section("barStatus", resolution))
    bar_resources = BarResources(gameWindow, config_loader.get_section("barResources", resolution))
    specialSkill = Skill(gameWindow, config_loader.get_section("skill", resolution), "specialSkill")
    skill1 = Skill(gameWindow, config_loader.get_section("skill", resolution), "specializeSkill1")
    skill4 = Skill(gameWindow, config_loader.get_section("skill", resolution), "specializeSkill4")

    print('初始化完成')
    while True:
        key_event.wait()  # 等待事件被设置

        print('启动')

        while side_button_pressed:
            # 居合还没好就先等居合好
            if not specialSkill.is_ready():
                time.sleep(0.1)
                continue

            # 居合好了按居合
            while specialSkill.is_ready() and side_button_pressed:
                keyboard_controller.tap('1')
                print('居合')
                time.sleep(0.05)
            time.sleep(0.3)

            # 检查超高出力层数
            skill1_num = skill1.get_value()
            if side_button_pressed:
                # 检查是否在无穷雷霆之力持续期间
                if bar_status.is_burst():
                    print('无穷持续中')

                    # 无穷期间的循环就是居合一闪，多的事情不要做
                    if skill4.is_ready():
                        keyboard_controller.tap('5')
                        print('一闪')
                        time.sleep(0.4)

                    # 一闪完没回豆的话必须要超高补豆，不然会断轴
                    if not bar_resources.have_resource_of_num(3) and skill1_num > 0 and side_button_pressed:
                        time.sleep(0.2)
                        keyboard_controller.tap('2')
                        print('超高出力')
                else:

                    print('无穷结束')
                    # 没有无穷，核心思路就是，打三豆给居合用，居合三豆有概率送一个一闪，一闪又有概率送一个豆子，三豆居合能够最大限度保下限
                    # 所以有一闪就打一闪，暴击能回一个豆给居合用
                    if skill4.is_ready():
                        keyboard_controller.tap('5')
                        print('一闪')
                        time.sleep(0.4)

                    # 超高有层数就用，避免层数溢出，选在一闪后进行超高是因为超高回的豆必须给居合用
                    elif skill1_num > 1 and side_button_pressed:
                        keyboard_controller.tap('2')
                        print('超高出力')
                        time.sleep(0.1)

                    # 加个普工触发看破斩，看破斩可以让超高一次回三个豆子，能够直接进行三豆居合
                    mouse_controller.click(mouse.Button.left, 1)
                    print('普攻')

        key_event.clear()  # 清除事件


# 启动鼠标监听线程
mouse_thread = threading.Thread(target=mouse_listener, daemon=True)
mouse_thread.start()

# 启动按键循环线程
key_press_thread = threading.Thread(target=key_press_loop, daemon=True)
key_press_thread.start()

# 保持主程序运行
mouse_thread.join()
key_press_thread.join()
